<?php

/**
 * 从这里开始，使用了一个新的表：live
 */

require '../vendor/autoload.php';

use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\DynamoDb\Marshaler;

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest',
    'credentials' => [
        'key' => 'AKIA4SBKFCJAXUOUXUN2',
        'secret' => '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',
    ],
]);

$dynamodb = $sdk->createDynamoDb();
$marshaler = new Marshaler();

$tableName = 'live';

//插入50条随机数据
for ($i = 0; $i < 50; $i++) {
    $channel_id = uniqid();
    $count = mt_rand(0, 9999);
    $scheduleTime = time() + mt_rand(300, 10 * 24 * 60 * 60);
    $statusArr = ['canceled', 'ended', 'upcoming', 'living'];
    $status = $statusArr[array_rand($statusArr)];

    $item = $marshaler->marshalItem([
        'channel_id' => $channel_id,
        'schedule_time' => $scheduleTime,
        'count' => $count,
        'status' => $status
    ]);

    $params = [
        'TableName' => 'live',
        'Item' => $item,
    ];

    try {
        $result = $dynamodb->putItem($params);
        echo "Added item success" . PHP_EOL;
    } catch (DynamoDbException $e) {
        echo "Unable to add item:\n";
        echo $e->getMessage() . "\n";
    }
}
